//全局音频类
var GlobalAudio = function($item) {
	//定义属性
	this._$globalAudio = $item; //容器对象
	this._$tip = $('<span></span>'); //文本提示容器
	this.audio = this._$globalAudio.find('audio')[0]; //获取音频控件
	this.isAllowManually = true; //是否允许手动操作
	this.playState = 'ready'; //当前播放状态

	//定义临时变量
	var theClass = this;

	//添加文本提示容器
	this._$globalAudio.append(this._$tip);

	//加载完成时自动播放
	if (this.audio.autoplay) {
		// this.audio.pause();
		setTimeout(function() {
			theClass.play();
		}, 2000);
	}

	//加载完成后才允许手动控制播放
	theClass.isAllowManually = true;
	//播放控制
	this._$globalAudio.on('click', function(e) {
		e.preventDefault();
		if (theClass.isAllowManually) {
			theClass._$globalAudio.is('.z-play') ? theClass.pause() : theClass.play();
		}
	});

	//修复ios/android 4.4下音频不播放的问题
	$(document).one('touchstart', function(e) {
		theClass.audio.play();
	});
};

//播放
GlobalAudio.prototype.play = function() {
	if (!this._$globalAudio.is('.z-play')) {

		this.audio.play();
		this._$globalAudio.removeClass('z-pause').addClass('z-play');
		this._showTip('开启');
		this.playState = 'playing';
	}
};

//暂停
GlobalAudio.prototype.pause = function() {
	if (!this._$globalAudio.is('.z-pause')) {
		this.audio.pause();
		this._$globalAudio.removeClass('z-play').addClass('z-pause');
		this._showTip('关闭');
		this.playState = 'pause';
	}
};

//显示提示
GlobalAudio.prototype._showTip = function(msg) {
	var theClass = this;
	this._$tip.text(msg);
	this._$tip.addClass('z-show');
	setTimeout(function() {
		theClass._$tip.removeClass('z-show');
	}, 1000);
};

//获取全局音频容器
var globalAudio = new GlobalAudio($('.u-globalAudio'));

var loadingPage = (function (sourceArr) {
	for (var i = 0; i < sourceArr.length; i++) {
		sourceArr[i] = (sourceArr[i]);
	}
	var loadImage = function (path, callback) {
		var img = new Image();
		img.onload = function () {
			img.onload = null;
			callback(path);
		}
		img.src = path;
	}
	var imgLoader = function (imgs, callback) {
		var len = imgs.length, i = 0;
		while (imgs.length) {
			loadImage(imgs.shift(), function (path) {
				callback(path, ++i, len);
			});
		}
	}

	imgLoader(sourceArr, function (path, curNum, total) {
		var percent = curNum / total;
		$('.num').text(Math.floor(percent * 100) + '%')
		if (percent == 1) {
			$('.num').text('正在进入盛典现场...')
			setTimeout(function () {
				$('#J_loading').hide();
				$(".flipbook-viewport").show();
			}, 1200);
		}
	});
});

var rootUrl = window.LOADING_IMAGE_ROOT_URL
loadingPage([
	rootUrl + 'img/cover.gif',
	rootUrl + 'img/america.gif',
	rootUrl + 'img/australia.gif',
	rootUrl + 'img/britain.gif',
	rootUrl + 'img/denmark.gif',
	rootUrl + 'img/france.gif',
	rootUrl + 'img/germany.gif',
	rootUrl + 'img/italy.gif',
	rootUrl + 'img/japan.gif',
	rootUrl + 'img/korea.gif',
	rootUrl + 'img/cover.gif',
	rootUrl + 'img/units-icons.png'
])

//屏蔽ios下上下弹性
$(window).on('scroll.elasticity', function (e) {
	e.preventDefault();
}).on('touchmove.elasticity', function (e) {
	e.preventDefault();
});

// 初始化定位
$('.loadinner').css('top', (($(window).height() - 265) / 2) + 'px').css('visibility', 'visible');

function loadApp() {
	var w = $(window).width();
	var h = $(window).height();
	$('.flipboox').width(w).height(h);
	$(window).resize(function () {
		w = $(window).width();
		h = $(window).height();
		$('.flipboox').width(w).height(h);
	});
	$('.flipbook').turn({
		// Width
		width: w,
		// Height
		height: h,
		// Elevation
		//elevation: 60,
		duration: 800,
		display: 'single',
		// Enable gradients
		gradients: true,
		turnCorners: 'bl,tr',
		// Auto center this flipbook
		autoCenter: true
	});

	$('.right_arrow').bind('click', function () {
		$('.flipbook').turn("next");
	});
	$('.left_arrow').bind('click', function () {
		$('.flipbook').turn("previous");
	});

	$(".flipbook").bind("turning", function (event, page, view) {
		if (page == 1) {
			$('.left_arrow').hide();
		} else if (page == 11) {
			$('.right_arrow').hide();
		} else {
			$('.left_arrow').show();
			$('.right_arrow').show();
		}
	});
}
// Load the HTML4 version if there's not CSS transform
yepnope({
	test: Modernizr.csstransforms,
	yep: ['js/components/turn.js'],
	nope: ['js/components/turn.html4.min.js'],
	complete: loadApp
});

